package com.cj.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cj.commonutils.R;
import com.cj.staservice.client.courseClient;
import com.cj.staservice.client.ucenterClient;
import com.cj.staservice.entity.StatisticsDaily;
import com.cj.staservice.mapper.StatisticsDailyMapper;
import com.cj.staservice.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-21
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private ucenterClient ucenterClient;

    @Autowired
    private courseClient courseClient;

    @Override
    public void registerCount(String day) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        R registerR = ucenterClient.getCountRegister(day);
        R LoginR = ucenterClient.getCountLogin(day);
        R ViewR = courseClient.getCountView(day);
        Integer getRegister = (Integer) registerR.getData().get("countRegister");
        Integer getLogin = (Integer) LoginR.getData().get("countLogin");
        Integer getView = (Integer) ViewR.getData().get("countView");
        //把获取数据添加数据库，统计分析表里面
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(getRegister); //注册人数
        sta.setDateCalculated(day);//统计日期
        sta.setLoginNum(getLogin);//登录人数
        sta.setVideoViewNum(getView);//观看人数
        sta.setCourseNum(RandomUtils.nextInt(1, 5));//随机每日新增课数
        baseMapper.insert(sta);

    }

    @Override
    public void LoginCount(String day) {

    }

    @Override
    public void viewCount(String day) {

    }
    //根据条件显示图表
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            //封装日期list集合
            date_calculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", date_calculatedList);
        map.put("numDataList", numDataList);
        return map;
    }
    //直接显示图表
    @Override
    public Map<String, Object> showDateData(String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.orderByAsc("date_calculated");
        //wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> LoginNumDataList = new ArrayList<>();
        List<Integer> RegisterNumDataList = new ArrayList<>();
        List<Integer> VideoViewNumList = new ArrayList<>();
        List<Integer> CourseNumDataList = new ArrayList<>();
        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            //封装日期list集合
            date_calculatedList.add(daily.getDateCalculated());
            LoginNumDataList.add(daily.getLoginNum());
            RegisterNumDataList.add(daily.getRegisterNum());
            VideoViewNumList.add(daily.getVideoViewNum());
            CourseNumDataList.add(daily.getCourseNum());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", date_calculatedList);
        map.put("LoginNumDataList", LoginNumDataList);
        map.put("RegisterNumDataList", RegisterNumDataList);
        map.put("VideoViewNumList", VideoViewNumList);
        map.put("CourseNumDataList", CourseNumDataList);
        return map;
    }


}
